﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Nop.Core;
using Nop.Core.Domain.Customers;
using Nop.Core.Domain.Catalog;
using Nop.Core.Data;
using Nop.Core.Caching;
using Nop.Core.Events;

namespace Nop.Services.Customers
{
    public partial class ShopCollectService:IShopCollectService
    {
                #region Constants

        private const string Era_ShopCollect_BY_ID_KEY = "Nop.Era_ShopCollect.id-{0}";
        private const string Era_ShopCollect_PATTERN_KEY = "Nop.Era_ShopCollect.";

        #endregion

        #region Fields

        private readonly IRepository<ShopCollect> _era_ShopCollectRepository;
        private readonly ICacheManager _cacheManager;
        private readonly IEventPublisher _eventPublisher;
        private readonly IRepository<Manufacturer> _manufacturerRepository;
        #endregion

        #region Cotr

        public ShopCollectService(IRepository<ShopCollect> era_ShopCollectRepository, IRepository<Manufacturer> manufactureRepository, ICacheManager cacheManager, IEventPublisher eventPublisher)
        {
            this._era_ShopCollectRepository = era_ShopCollectRepository;
            this._manufacturerRepository = manufactureRepository;
            this._cacheManager = cacheManager;
            this._eventPublisher = eventPublisher;
        }

        #endregion

        #region method

        public void InsertEntity(ShopCollect entity)
        {
            if (entity == null)
            {
                throw new ArgumentNullException("Era_ShopRecommend");
            }

            _era_ShopCollectRepository.Insert(entity);
            _cacheManager.RemoveByPattern(Era_ShopCollect_PATTERN_KEY);
            _eventPublisher.EntityInserted(entity);
        }

        public ShopCollect GetEntityByFunc(Func<ShopCollect, bool> whereLambda)
        {
            var entity = _era_ShopCollectRepository.Table.Where(whereLambda).FirstOrDefault();
            return entity;
        }


        /// <summary>
        /// 根据用户ID和收藏的ID，删除信息
        /// </summary>
        /// <param name="recommendId"></param>
        /// <param name="userId"></param>
        public void DeleteCollect(int collectId, int userId)
        {
            var model = _era_ShopCollectRepository.Table.Where(c => c.Id == collectId && c.UserId == userId).FirstOrDefault();
            if (model != null)
            {
                _era_ShopCollectRepository.Delete(model);
            }
        }

        public ShopCollect GetCollectById(int recommendId)
        {
            var model = _era_ShopCollectRepository.GetById(recommendId);
            return model;
        }

        /// <summary>
        /// 分页获取数据
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="pagesize"></param>
        /// <param name="pageindex"></param>
        /// <param name="recommendType"></param>
        /// <returns></returns>
        public IPagedList<Manufacturer> GetListByUserId(int userId, int pagesize, int pageindex, int recommendType)
        {
            var query = _manufacturerRepository.Table.Where(c => c.Published == true);
            query = query.Join(_era_ShopCollectRepository.Table, x => x.Id, y => y.CollectId, (x, y) => new { Manufacturer = x, ShopRecommend = y })
                  .Where(z => z.ShopRecommend.UserId==userId)
                  .Select(z => z.Manufacturer);
            query = query.OrderBy(c => c.CreatedOnUtc);
            var shopRecommend = new PagedList<Manufacturer>(query, pageindex, pagesize);
            return shopRecommend;
        }
        #endregion
    }
}
